package ace.tool.generate.code.impl.resolver.scheme.resolver.core;

import ace.tool.generate.code.impl.manager.common.EntityModelManager;
import ace.tool.generate.code.impl.resolver.enums.RestTenantAdminEntityOfAppEnum;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.EntityClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.bo.core.RestTenantAdminControllerClassInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ClassFileInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.resolver.scheme.model.dto.ProjectInfo;
import ace.tool.generate.code.impl.resolver.scheme.resolver.ClassFileInfoResolver;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import ace.tool.generate.code.impl.service.model.input.GenerateModelInput;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@RequiredArgsConstructor
public class RestTenantAdminControllerClassInfoResolver {

  private final GenerateInput input;
  private final GenerateModelInput modelInput;
  private final List<EntityClassInfo> entityClassInfos;
  private final ProjectInfo coreProjectInfo;
  private final CommonInfo commonInfo;

  public List<RestTenantAdminControllerClassInfo> resolve() {
    if (!Boolean.TRUE.equals(modelInput.getFrameworkBizEntityOfAppFlag())) {
      return new ArrayList<>();
    }

    return this.modelInput
        .getRestTenantAdminEntityOfAppEnumList()
        .stream()
        .map(this::getRestTenantAdminControllerClassInfo)
        .collect(Collectors.toList());

  }

  private RestTenantAdminControllerClassInfo getRestTenantAdminControllerClassInfo(RestTenantAdminEntityOfAppEnum restTenantAdminEntityOfAppEnum) {
    String entityModelId = EntityModelManager.getInstance().getEntityModelId(input, modelInput);
    EntityClassInfo entityClassInfo = EntityModelManager.getInstance().getEntityClassInfoByEntityModelId(this.entityClassInfos, entityModelId);
    ClassFileInfo classFileInfo = ClassFileInfoResolver.builder()
        .className(entityClassInfo.getModel().getClassName())
        .classNameSuffix(input.getRestTenantAdminControllerClassNameSuffix())
        .classPackagePrefix(input.getRestTenantAdminControllerClassPackagePrefix() + "." + restTenantAdminEntityOfAppEnum.getCode())
        .rootPackageFullName(commonInfo.getRootPackageFullName())
        .sourceCodeRootDir(coreProjectInfo.getSourceCodeRootDir())
        .build()
        .resolve();

    return RestTenantAdminControllerClassInfo.builder()
        .entityModelId(entityModelId)
        .info(classFileInfo)
        .restNamespace(input.getProjectName())
        .build();
  }
}
